import { GLTFLoader } from "../threeex/GLTFLoader";
import {
    MODEL_GLTF_LIST,
    TEXTURE_LIST,
    FONT_LIST,
    SHADER_LIST
} from "../../stage3d/fight/reslist";


export default class RescacheMgr {
    constructor() {
        this._preload_idx = 0
        this._preload_cb = function () { }
        this._load_mgr = new THREE.LoadingManager()


        this._model_map = {}
        this._tp_map = {}
        this._font_map = {}
        this._glsl_map = {}
    }

    static getInstance() {
        if (!this._inc) {
            this._inc = new RescacheMgr();
        }
        return this._inc;
    }

    cloneGLTF(modkey) {
        return this._model_map[modkey].clone();
    }

    getTexture(tpkey) {
        return this._tp_map[tpkey];
    }

    getGLSL(slfkey) {
        return this._glsl_map[slfkey];
    }

    preload(cb) {
        if (typeof (cb) == "function") {
            this._preload_cb = cb;
        }
        this._load_tplist();
        this._load_modellist();
        this._load_fontlist();
        this._load_shaderlist();
        return this;
    }

    _add_modelcache(mapkey, obj) {
        if (this._model_map[mapkey]) {
            return;
        }
        this._model_map[mapkey] = obj.scene.children[0];
    }

    _add_tpcache(mapkey, obj) {
        if (this._tp_map[mapkey]) {
            return;
        }
        this._tp_map[mapkey] = obj;
    }

    _add_fontcache(mapkey, obj) {
        if (this._font_map[mapkey]) {
            return;
        }
        this._font_map[mapkey] = obj;
    }

    _add_glslcache(mapkey, cont) {
        if (this._glsl_map[mapkey]) {
            return
        }
        this._glsl_map[mapkey] = cont;
    }

    _load_tplist(cb) {
        let loader = new THREE.TextureLoader();
        let nidx = 0;
        const mapcnt = Object.keys(TEXTURE_LIST).length;

        //tp 
        for (const tpname in TEXTURE_LIST) {
            if (TEXTURE_LIST.hasOwnProperty(tpname)) {
                const element = TEXTURE_LIST[tpname];
                loader.load(element, (tp) => {
                    this._add_tpcache(tpname, tp)
                    nidx += 1;
                    if (nidx == mapcnt) {
                        this._chk_execute_cb();
                    }
                })
            }
        }
    }

    _load_fontlist() {
        let loader = new THREE.FontLoader();
        let nidx = 0;
        const mapcnt = Object.keys(FONT_LIST).length;

        //font 
        for (const fname in FONT_LIST) {
            if (FONT_LIST.hasOwnProperty(fname)) {
                const element = FONT_LIST[fname];
                loader.load(element, (tp) => {
                    this._add_fontcache(fname, tp)
                    nidx += 1;
                    if (nidx == mapcnt) {
                        this._chk_execute_cb();
                    }
                })
            }
        }
    }

    _load_modellist() {
        let loader = new GLTFLoader();
        let nidx = 0;
        const mapmcnt = Object.keys(MODEL_GLTF_LIST).length;

        //gltf
        for (const model in MODEL_GLTF_LIST) {
            if (MODEL_GLTF_LIST.hasOwnProperty(model)) {
                const element = MODEL_GLTF_LIST[model];
                loader.load(element, (gltf) => {
                    this._add_modelcache(model, gltf);
                    nidx += 1;
                    if (nidx == mapmcnt) {
                        this._chk_execute_cb();
                    }
                })
            }
        }
    }

    _load_shaderlist() {
        let loader = new THREE.FileLoader();
        let nidx = 0;
        const mapmcnt = Object.keys(SHADER_LIST).length;

        //gltf
        for (const model in SHADER_LIST) {
            if (SHADER_LIST.hasOwnProperty(model)) {
                const element = SHADER_LIST[model];
                loader.load(element, (gltf) => {
                    this._add_glslcache(model, gltf);
                    nidx += 1;
                    if (nidx == mapmcnt) {
                        this._chk_execute_cb();
                    }
                })
            }
        }
    }

    _chk_execute_cb() {
        this._preload_idx += 1;
        if ((this._preload_idx) == 4) {
            this._preload_cb();
        }
    }

    update() {
    }
}